
#include <stdio.h>
#include <stdlib.h>

// singly node type
struct snode {
    int data;
    struct snode *next;
};
typedef struct snode sNode_t;


static sNode_t *newNode(int x) {
    sNode_t *node = (sNode_t *)malloc(sizeof(sNode_t));
    if (node != NULL) {
        node->data = x;
        node->next = NULL;
    }
    return node;
}

//insert x at front
void insertAtFront(sNode_t **head, int x) {
    sNode_t *node = newNode(x);
    if (!node) return;

    if ( !*head ) { //head==NULL
        *head = node;
        node->next = *head;
    } else {
        sNode_t *tailNode = *head;
        while(tailNode->next != *head) 
            tailNode = tailNode->next;
        node->next = *head;
        tailNode->next = node; 
        *head = node;
    }
} 


//insert x at front
sNode_t * __insertAtFront(sNode_t *head, int x) {
    sNode_t *node = newNode(x);
    if (!node) return head;

    if ( !head ) { //head==NULL
        head = node;
        node->next = head;
    } else {
        sNode_t *tailNode = head;
        while(tailNode->next != head) 
            tailNode = tailNode->next;
        node->next = head;
        tailNode->next = node; 
        head = node;
    }
    return head;
} 


void printList(sNode_t *head) {
    sNode_t *ptrNode = head;

    if (!head) {
        printf("list is empty\n");
        return;
    }
    while(ptrNode->next != head) {
        printf("%d ", ptrNode->data);
        ptrNode = ptrNode->next;
    }
    printf("%d\n", ptrNode->data);
}

int main() {
    sNode_t *list=NULL;


   for(int i=1;i<10;i++) {
        list = __insertAtFront(list, i);
    }
    printList(list);

}